package leetcode_day._2021._202103;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author yzh
 * @data 2021/3/20 11:59
 * 逆波兰表达式求值
 * 思路：
 *   使用栈
 *   如果是数字，则直接入栈
 *   如果是运算符号，则从栈中取出两个数运算
 */
public class _20_150 {

    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new ArrayDeque<>();
        for (String s : tokens) {
            if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
                int top = stack.pop(), bottom = stack.pop();
                switch(s) {
                    case "+": stack.push(bottom + top);break;
                    case "-": stack.push(bottom - top);break;
                    case "*": stack.push(bottom * top);break;
                    case "/": stack.push(bottom / top);break;
                }
            } else {
                stack.push(Integer.parseInt(s));
            }
        }
        return stack.pop();
    }
}
